import { assetsUrl } from "../../config/env";
import { getGoodsList } from "../../apis/goods";
const app = getApp();

Page({
  data: {
    assetsUrl,
    statusBarHeight: 0,
    searchKeyword: "", // 用户输入的搜索关键词
    searchResults: [], // 搜索结果列表
    list1: [],
    list2: [],
    list3: [],
    list4: [],
    list5: [],
  },
  async getGoods(categoryId) {
    let params = {
      pageNum: 1,
      pageSize: 4,
    };
    if (categoryId) {
      params.categoryId = categoryId;
    }
    const res = await getGoodsList(params);
    return res.list;
  },
  // 监听用户输入
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value,
    });
  },

  // 点击搜索按钮触发
  onSearch() {
    const keyword = this.data.searchKeyword.trim();
    if (!keyword) {
      wx.showToast({
        title: "请输入商品名称",
        icon: "none",
      });
      return;
    }

    // 模拟搜索结果（实际开发中需要调用后端接口）
    const mockData = [
      { name: "商品A", price: 100 },
      { name: "商品B", price: 200 },
      { name: "商品C", price: 300 },
    ];

    // 过滤出匹配的商品
    const results = mockData.filter((item) => item.name.includes(keyword));
    this.setData({
      searchResults: results,
    });

    if (results.length === 0) {
      wx.showToast({
        title: "未找到相关商品",
        icon: "none",
      });
    }
  },

  async onLoad() {
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight,
    });
    this.setData({
      list1: await this.getGoods(1),
      list2: await this.getGoods(2),
      list3: await this.getGoods(3),
      list4: await this.getGoods(4),
      list5: await this.getGoods(5),
    });
    // await login('1223')
  },
  gotoCategory(options) {
    const tab = options.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../search/index?tab=" + tab,
    });
  },
  gotoDetail(e) {
    console.log(e);
    
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/detail/index?id=" + id,
    });
  },
});
